<template>
  <div class="login">
    <div class="tab">
      <p
        class="tab-item tab-pwd"
        :class="[active === 0 && 'active']"
        @click="tab(0)"
      >
        账号密码登录
      </p>
      <p
        class="tab-item tab-code"
        :class="[active === 1 && 'active']"
        @click="tab(1)"
      >
        验证码登录
      </p>
    </div>
    <div class="tab-current" v-if="active === 0">
      <div class="login-wrap login-pwd">
        <div class="phone clearfix">
          <p>+0086</p>
          <input v-model="mobile" name="mobile" type="tel" placeholder="请输入手机号" />
        </div>
        <div class="pwd">
          <input
            v-model="password"
            name="password"
            @keyup.enter="submit"
            :type="activePwd ? 'text' : 'password'"
            placeholder="请输入密码"
          />
          <i
            class="iconfont"
            :class="[activePwd ? 'icon-eye-open' : 'icon-eye-close']"
            @click="activePwd = !activePwd"
          ></i>
        </div>
      </div>
    </div>
    <div class="tab-current" v-else>
      <div class="login-wrap login-code">
        <div class="phone clearfix">
          <p>+0086</p>
          <input v-model="mobile" name="mobile" type="tel" placeholder="请输入手机号" />
        </div>
        <div class="code">
          <input
            v-model="msgCode"
            @keyup.enter="submit"
            type="text"
            name="code"
            placeholder="请输入验证码"
          />
          <MsgCode :phone-number="mobile" />
        </div>
      </div>
    </div>
    <div class="operate">
      <div class="auto">
        <label @click="toCheck">
          <span class="checkbox" v-show="checked">
            <i class="el-icon-check"></i>
          </span>
          <span class="checkbox" v-show="!checked">&nbsp;</span>
          <span class="text">下次自动登录</span>
        </label>
        <p class="toMiss" @click="toMiss">忘记密码</p>
        <p class="toRegister" @click="toRegister">注册</p>
      </div>
    </div>
    <div class="submit">
      <button type="button" @click="submit">登录</button>
    </div>
  </div>
</template>

<script>
import { setUser } from '@/utils/cookie';
import { msgLogin, pwdLogin } from '@/api/users';

export default {
  data () {
    return {
      active: 0,
      mobile: '',
      password: '',
      msgCode: '',
      showCodeBtn: true,
      count: null,
      checked: false,
      activePwd: false,
    }
  },
  created () {
  },
  mounted () {
  },
  methods: {
    tab (active) {
      this.active = active;
    },
    toCheck () {
      this.checked = !this.checked;
    },
    toRegister () { // 注册
      this.$store.dispatch('user/toggleModal', {
        register: true
      })
    },
    toMiss () { // 忘记密码
      this.$store.dispatch('user/toggleModal', {
        miss: true
      })
    },
    setCountDown () { // 倒计时
      this.count = 60;
      this.showCodeBtn = false;
      let timer = setInterval(() => {
        this.count--;
        if (this.count === 0) {
          this.showCodeBtn = true;
          this.count = null;
          clearInterval(timer);
          timer = null;
        }
      }, 1000);
    },
    loginByPwd (mobile, password) { // 密码登录
      pwdLogin({
        mobile,
        password,
        autologin: this.checked ? 1 : 0
      }).then(res => {
        this.handelLogin(res);
      }).catch(err => {
        console.log(err)
        this.$message.error(err || '网络错误, 稍后再试！');
      })
    },
    loginByMsg (mobile, code) { // 短信验证码登录
      msgLogin({
        mobile,
        code,
        autologin: this.checked ? 1 : 0
      }).then(res => {
        this.handelLogin(res);
      }).catch(err => {
        console.log(err)
        this.$message.error(err || '网络错误, 稍后再试！');
      })
    },
    handelLogin (res) { // 处理登录
      if (res.code === 200 || res.code === 1003 || res.code === 1004) {
        this.$message.success(res.msg || '登录成功！');
        this.toSetCookie(res.data);
        this.$store.dispatch('user/userInfo', res.data);
      } else {
        this.$message.error(res.msg || '网络错误, 稍后再试！');
      }
    },
    submit () { // 登录
      const mobile = this.mobile || '';
      const password = this.password || '';
      const msgCode = this.msgCode || '';
      if (!mobile || !/^1[3456789]\d{9}$/.test(mobile)) {
        this.$message.warning('请正确填写手机号码！')
        return false
      }
      if (this.active === 0) { // 密码登录
        if (!password) {
          this.$message.warning('请填写密码！')
          return false
        }
        this.loginByPwd(mobile, password);
      } else if (this.active === 1) { // 短信验证码登录
        if (!msgCode) {
          this.$message.warning('请填写短信验证码！')
          return false
        }
        this.loginByMsg(mobile, msgCode);
      }
    },
    toSetCookie (data) {
      data.checked = this.checked;
      this.checked ? setUser(JSON.stringify(data), 30) : setUser(JSON.stringify(data));
      this.$store.dispatch('user/toggleModal', {})
    },
  }
}
</script>
<style lang="scss" scoped>
.login {
  height: 430px;
  padding: 50px;
  .tab {
    height: 50px;
    p {
      line-height: 49px;
      width: 120px;
      text-align: center;
      font-size: 18px;
      color: #333;
      border-bottom: 1px solid transparent;
      &:hover {
        cursor: pointer;
        color: $base-color;
      }
      &.active {
        border-color: $base-color;
      }
      &.tab-pwd {
        float: left;
      }
      &.tab-code {
        float: right;
      }
    }
  }
  .login-wrap {
    margin: 30px 0;
    .phone,
    .pwd,
    .code {
      height: 55px;
      border-bottom: 1px solid #eee;
      input,
      p {
        line-height: 54px;
        height: 54px;
        color: #333;
        font-size: 16px;
      }
    }
    .pwd {
      position: relative;
      i {
        position: absolute;
        right: 0;
        top: 5px;
        font-size: 25px;
        padding: 10px;
        color: #999;
        &:hover {
          cursor: pointer;
          color: #333;
        }
      }
    }
    .phone {
      p {
        float: left;
        width: 80px;
      }
    }
  }
  .operate {
    .auto {
      p {
        float: right;
        margin-left: 25px;
        color: #999;
        font-size: 16px;
        &:hover {
          color: $base-color;
          cursor: pointer;
        }
      }
    }
  }
  .submit {
    margin-top: 40px;
    text-align: center;
    button {
      height: 45px;
      width: 335px;
      margin: 0 auto;
      background: $base-color;
      color: #fff;
      border: 1px solid $base-color;
      border-radius: 22.5px;
      font-size: 16px;
      &:hover {
        background: #fff;
        color: $base-color;
      }
    }
  }
}
</style>
